package belajarzk.action.maintenance.helper;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.sys.ExecutionsCtrl;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Center;
import org.zkoss.zul.South;
import org.zkoss.zul.Caption;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Groupbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treechildren;
import org.zkoss.zul.Treecol;
import org.zkoss.zul.Treecols;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Treerow;
import org.zkoss.zul.Window;

import belajarzk.common.Common;
import belajarzk.database.hibernate.HibernateUtil;
import belajarzk.database.model.Menu;
import belajarzk.database.model.Tbmrole;
import belajarzk.ui.render.grid.TbmroleRenderer;
import belajarzk.ui.util.MyTolbarbutton;


public class MenuSelecter {

	private Map<Integer, Object[]> checkboxs = new HashMap<Integer, Object[]>();
	private Window window;
	private Tbmrole job;
	private Groupbox groupbox;

	public synchronized static MenuSelecter createInstance(Tbmrole job,
			Groupbox groupbox) throws Exception {

		// System.out.println("job menu = " + job.getMenus().size());
		MenuSelecter menuSelecter = new MenuSelecter();
		menuSelecter.window = ((Window) ExecutionsCtrl.getCurrentCtrl()
				.getCurrentPage().getVariable("addMenu"));
		menuSelecter.job = job;
		menuSelecter.groupbox = groupbox;
		menuSelecter.init();
		menuSelecter.window.setVisible(true);
		menuSelecter.window.onModal();
		return menuSelecter;
	}

	private MenuSelecter() {

	}

	private synchronized static Boolean hasChild(Integer root, List<Menu> menus) {
		for (Menu menu : menus) {
			if (menu.getRoot().equals(root)) {
				return true;
			}
		}
		return false;
	}

	private void createTreerow(Treeitem treeitem, final Menu menu) {
		Treerow treerow = new Treerow();
		new Treecell(menu.getLabel()).setParent(treerow);

		Treecell treecell = new Treecell();
		final Checkbox checkbox = new Checkbox();
		if (job != null && job.getMenus() != null) {
			for (Menu myMenu : job.getMenus()) {
				if (menu.getId().equals(myMenu.getId())) {
					checkbox.setChecked(true);
					break;
				}
			}
		}
		checkboxs.put(new Double(1000000.0 * Math.random()).intValue(),
				new Object[] { checkbox, menu });
		checkbox.setParent(treecell);
		treecell.setParent(treerow);
		treerow.setParent(treeitem);
	}

	private void createRootSubMenu(Integer root, List<Menu> menus,
			Component componen) {
		Treechildren tc1 = new Treechildren();
		for (final Menu menu : menus) {
			if (menu.getRoot().equals(root)) {
				Boolean ada = hasChild(menu.getChild(), menus);
				if (ada) {
					Treeitem treeitem = new Treeitem();
					treeitem.setValue(menu.getUrl());
					createTreerow(treeitem, menu);
					treeitem.setParent(tc1);
					createRootSubMenu(menu.getChild(), menus, treeitem);
				} else {
					Treeitem treeitem = new Treeitem();
					treeitem.setValue(menu.getUrl());
					createTreerow(treeitem, menu);
					treeitem.setParent(tc1);
				}
			}
		}

		tc1.setParent(componen);
	}

	public void createTreeMenu(Tree tree, final List<Menu> menus) {

		Treecols treecols = new Treecols();
		Treecol treecol = new Treecol("Tree Menu");
		treecol.setWidth("95%");
		treecol.setParent(treecols);
		treecol = new Treecol("");
		treecol.setWidth("5%");
		treecol.setParent(treecols);
		treecols.setParent(tree);

		Treechildren tc1 = new Treechildren();
		for (final Menu menu : menus) {
			if (menu.getRoot().equals(0)) {
				Treeitem treeitem = new Treeitem();
				treeitem.setValue(menu.getUrl());
				createTreerow(treeitem, menu);
				treeitem.setParent(tc1);
				createRootSubMenu(menu.getChild(), menus, treeitem);
			}
		}
		tc1.setParent(tree);
	}

	@SuppressWarnings("unchecked")
	private void save() throws Exception {
		job.setMenus(new HashSet<Menu>());

		for (Object[] objs : this.checkboxs.values()) {
			Checkbox combobox = (Checkbox) objs[0];
			Menu myMenu = (Menu) objs[1];
			Boolean check = combobox.isChecked();
			System.out.println("menu = " + myMenu.getLabel() + ", check = "
					+ check);
			if (check) {

				job.getMenus().add(myMenu);
			}

		}
		Session session = HibernateUtil.currentSession();
				
		try {
			
			session.update(session.merge(job));
			

			List list = groupbox.getChildren();
			((Component) list.get(0)).detach();
			((Component) list.get(0)).detach();

			final Tree mytree = TbmroleRenderer.createTree(job, groupbox);
			mytree.setVisible(true);
			TbmroleActionHelper.createTreeMenu(groupbox, mytree, job);

		} catch (Exception e) {
			e.printStackTrace();
			
			Messagebox.show("Data ini tidak bisa ditambahkan ..");
		}

		
		
		window.setVisible(false);
	}

	@SuppressWarnings("unchecked")
	private void init() {

		Common.clear(window);
		Borderlayout borderlayout = new Borderlayout();
		Center center = new Center();
		center.setFlex(true);

		Session session = HibernateUtil.currentSession();
				

		List<Menu> menus = session.createCriteria(Menu.class).list();
		final Tree tree = new Tree();
		createTreeMenu(tree, menus);
		tree.setParent(center);

		
		

		Caption caption = new Caption();
		MyTolbarbutton button = new MyTolbarbutton(" X ");
		button.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event arg0) throws Exception {
				window.setVisible(false);
			}

		});
		button.setParent(caption);

		center.setParent(borderlayout);

		South south = new South();
		south.setFlex(true);
		Toolbar hbox = new Toolbar();
		hbox.setHeight("40px");
		button = new MyTolbarbutton(Common.getBahasa("common_cancel"), "/img/cancel.gif");
		
		button.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event arg0) throws Exception {
				window.setVisible(false);
			}

		});
		button.setParent(hbox);

		button = new MyTolbarbutton(Common.getBahasa("common_save"), "/img/save.gif");

		button.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event arg0) throws Exception {
				save();
			}

		});
		button.setParent(hbox);

		hbox.setParent(south);

		south.setParent(borderlayout);

		borderlayout.setParent(window);
		caption.setParent(window);
	}

}
